Semaphore Object Overview

The RTSS semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decreased by one each time a thread completes a wait for the semaphore object; the count is increased by a variable amount at the semaphore release. When the count reaches zero, the semaphore object's state is no longer signaled and no more threads can complete a wait on the semaphore object until some thread increases the count.

Like Windows semaphore objects, the RTSS semaphore objects are used for resource counting. They give a thread the ability to query the number of resources available; if one or more resources are available, the count of available resources is decreased. RTSS semaphores perform this test-and-set operation atomically; that is, when you request a resource from an RTSS semaphore, the operating system checks whether the resource is available and decrements the count of available resources without letting another thread interfere. Only after the resource count has been decreased does the system allow another thread to request a resource.

Because several threads can affect an RTSS semaphore's resource count, an RTSS semaphore, unlike an RTSS mutex, cannot be owned by a thread. This means that waiting for semaphore objects:

Semaphores and Thread Priority

On systems where all waiting threads are scheduled to run on the same RTSS processor, the RTSS semaphore objects support a waiting priority. That is, when there are several threads with different priority waiting for an RTSS semaphore object, the system guarantees that the order of threads obtaining the semaphore object is the order of thread priority when the RTSS semaphore object is signaled.

NOTE: This behavior differs from the behavior of Windows semaphore objects, which do not support waiting priority.

When waiting threads are scheduled to run on different processors, waiting priority is not guaranteed. RtReleaseSemaphore will signal waiting threads in the order of thread priority, through an inter-processor interrupt. However, inter-processor interrupt latency is not under software control. As a result, it is possible that a lower priority thread may wake up earlier than a higher priority thread if the processor scheduled to run the lower priority thread receives the inter-processor interrupt first.

The RTSS semaphore object is always maintained in the RTSS environment and namespace. However, Windows programs may create, open, release, and wait for RTSS semaphores. This allows cooperation between RTSS and Windows processes. The RTSS semaphore name space is separate from the Windows semaphore name space.

Semaphore Synchronization

To synchronize threads running in multiple processes, including RTSS processes and Windows processes, a thread in each process must have its own process-relative handle to a single RTSS semaphore object. These handles can be obtained by calling either RtCreateSemaphore or RtOpenSemaphore.